package com.hxkj.controller;

import com.hxkj.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;


@Slf4j
public class importResourceMain {

    private static void test(String fileDir)throws Exception {
        List<File> fileList = new ArrayList<File>();
        File file = new File(fileDir);
        File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
        if (files == null) {// 如果目录为空，直接退出
            return;
        }
        // 遍历，目录下的所有文件
        for (File f : files) {
            if (f.isFile()) {
                fileList.add(f);
            } else if (f.isDirectory()) {
               // System.out.println(f.getAbsolutePath());
                test(f.getAbsolutePath());
            }
        }
        Connection connection =  connection();
        PreparedStatement pstmt = null;

        for (File f1 : fileList) {
            String filename = f1.getName();
            String filepath = f1.getAbsolutePath();
            String newfilepath = filepath.substring(3,filepath.length()).replace("\\","/");
            String newFileParth = new String(newfilepath);
            newfilepath = "/home/hxkj/upload/resources/123506/default/lib/"+newfilepath;
            String sourcePath = "/upload/resources/123506/default/lib/"+newFileParth;
            List<String> stringList =  IOUtils.readLines(new FileInputStream(f1));
            String content = "";
            for(String s : stringList){
                content += s;
            }
            String path = f1.getPath();
            String dataPath = FilenameUtils.getFullPath(path).replace("\\","/");
            String[] finalLength = dataPath.split("/");
            String name = finalLength[finalLength.length-1];
            Long id = 0L;


            log.info(finalLength[finalLength.length-1]);
           String sql = "insert into `t_resources` (`resources_name`, `file_name`, `is_directory`, `path`, `source`, `size`, `createor`, `modify_by`, `create_time`, `last_modified_date`, `site_id`, `parent_id`, `del_tag`, `status`, `resources_alias`) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
            pstmt = (PreparedStatement) connection.prepareStatement(sql);
            pstmt.setString(1, f1.getName());
            String surffix = newfilepath.substring(0, newfilepath.length()-filename.length());
            log.error("文件为{}",sourcePath);
            pstmt.setString(2, surffix);
            String finalData[] = sourcePath.split("/");
            String   name4 =  finalData[finalData.length-2];
           if(!FilenameUtils.getExtension(filename).equals("css") && !FilenameUtils.getExtension(filename).equals("js")){
                pstmt.setString(3, "2");
                pstmt.setString(4,newfilepath+filename);
                pstmt.setString(5,newfilepath+filename);
                pstmt.setString(6, "10");
                pstmt.setInt(7, 1);
                pstmt.setInt(8, 1);
                pstmt.setLong(9, System.currentTimeMillis());
                pstmt.setLong(10, System.currentTimeMillis());
                pstmt.setLong(11, 123506);
                pstmt.setLong(12,getLong(name4));
                pstmt.setLong(13,0);
                pstmt.setLong(14,0);
                pstmt.setString(15, MD5Utils.getMd5ByResources());
            }else{
                pstmt.setString(3, "1");
                pstmt.setString(4,sourcePath+filename);
                pstmt.setString(5,content);
                pstmt.setString(6, "10");
                pstmt.setInt(7, 1);
                pstmt.setInt(8, 1);
                pstmt.setLong(9, System.currentTimeMillis());
                pstmt.setLong(10, System.currentTimeMillis());
                pstmt.setLong(11, 123506);
                pstmt.setLong(12,getLong(name4));
                pstmt.setLong(13,0);
                pstmt.setLong(14,0);
                pstmt.setString(15, MD5Utils.getMd5ByResources());
            }
            //pstmt.execute();
        }
    }

    public static void main(String[] args)throws Exception {
        test("E:\\layui");
    }


    public static Connection connection(){
        Connection con = null; //定义一个MYSQL链接对象
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动
            con = DriverManager.getConnection("jdbc:mysql://192.168.100.14:3306/cms_management_dev", "root", "123qwe"); //链接本地MYSQL
            System.out.print("yes");
        } catch (Exception e) {
            System.out.print("MYSQL ERROR:" + e.getMessage());
        }
        return con;
    }


    public static  Long getLong(String name){
        Long id = 0l;
        if(name.equals("layui")) id = 338l;
        if(name.equals("css")) id = 339l;
        if(name.equals("modules")) id = 345l;
        if(name.equals("laydate")) id = 356l;
        if(name.equals("default")) id = 348l;
        if(name.equals("layer")) id = 347l;
        if(name.equals("default")) id = 349l;
        if(name.equals("font")) id = 340l;
        if(name.equals("images")) id = 341l;
        if(name.equals("face")) id = 344l;
        if(name.equals("lay")) id = 342l;
        if(name.equals("modules")) id = 343l;
        return id;
    }
}
